package edu.cs.usask.ca.silver.plugin.rcp.dialogs.create;

import org.apache.log4j.Logger;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import edu.cs.usask.ca.silver.definitions.Exposure;

/**
 * 
 * @author Yudi Xue
 * 
 */
public class CreateGroupDialog extends TitleAreaDialog {

	private Text groupNameText;
	private Text groupDescriptionText;
	private Combo privacySelection;
	private String groupName;
	private String groupDescription;
	private Exposure groupPrivacy = null;
	private final Exposure[] dataForPrivacySelectionBox = new Exposure[] {
			Exposure.PRIVATE, Exposure.PUBLIC };
	private static Logger logger = Logger.getLogger(CreateGroupDialog.class);

	public CreateGroupDialog(Shell parentShell) {
		super(parentShell);
	}

	@Override
	public void create() {
		super.create();
		setTitle("Create New User Group");
		setMessage("Set up a new Group", IMessageProvider.INFORMATION);
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		GridLayout layout = new GridLayout();
		layout.numColumns = 2;
		// layout.horizontalAlignment = GridData.FILL;
		parent.setLayout(layout);

		Group group = new Group(parent, SWT.NONE);
		GridData gridData = new GridData();
		gridData.horizontalIndent = 2;
		gridData.horizontalSpan = 2;
		gridData.verticalSpan = 3;
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = GridData.FILL;
		gridData.verticalAlignment = GridData.FILL;
		group.setLayoutData(gridData);
		group.setLayout(new GridLayout(2, false));
		Label label1 = new Label(group, SWT.NONE);
		label1.setText("Group Name");

		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		groupNameText = new Text(group, SWT.BORDER);
		groupNameText.setLayoutData(gridData);

		gridData = new GridData();
		Label label2 = new Label(group, SWT.NONE);
		label2.setText("Group Description");
		gridData.verticalAlignment = SWT.TOP;
		label2.setLayoutData(gridData);

		gridData = new GridData();
		groupDescriptionText = new Text(group, SWT.BORDER | SWT.WRAP
				| SWT.MULTI);
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		gridData.minimumHeight = groupDescriptionText.getLineHeight() * 5;
		groupDescriptionText.setLayoutData(gridData);
		Label label3 = new Label(group, SWT.NONE);
		label3.setText("Group Privacy");

		gridData = new GridData();
		gridData.grabExcessHorizontalSpace = true;
		gridData.horizontalAlignment = SWT.FILL;
		gridData.grabExcessVerticalSpace = true;
		privacySelection = new Combo(group, SWT.READ_ONLY);
		String[] input = new String[dataForPrivacySelectionBox.length];
		for (int i = 0; i < dataForPrivacySelectionBox.length; i++) {
			input[i] = dataForPrivacySelectionBox[i].name();
		}
		privacySelection.setItems(input);
		privacySelection.setLayoutData(gridData);
		privacySelection.select(1);

		return parent;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		GridData gridData = new GridData();
		gridData.verticalAlignment = GridData.FILL;
		gridData.horizontalSpan = 3;
		gridData.grabExcessHorizontalSpace = true;
		gridData.grabExcessVerticalSpace = true;
		gridData.horizontalAlignment = SWT.CENTER;

		parent.setLayoutData(gridData);
		// Create Add button
		// Own method as we need to overview the SelectionAdapter
		createOkButton(parent, OK, "Add", true);
		// Add a SelectionListener

		// Create Cancel button
		Button cancelButton = createButton(parent, CANCEL, "Cancel", false);
		// Add a SelectionListener
		cancelButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				setReturnCode(CANCEL);
				close();
			}
		});
	}

	protected Button createOkButton(Composite parent, int id, String label,
			boolean defaultButton) {
		// increment the number of columns in the button bar
		((GridLayout) parent.getLayout()).numColumns++;
		Button button = new Button(parent, SWT.PUSH);
		button.setText(label);
		button.setFont(JFaceResources.getDialogFont());
		button.setData(Integer.valueOf(id));
		button.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				if (isValidInput()) {
					okPressed();
				}
			}
		});
		if (defaultButton) {
			Shell shell = parent.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
		setButtonLayoutData(button);
		return button;
	}

	private boolean isValidInput() {
		saveInput();
		boolean valid = true;
		if (groupNameText.getText().isEmpty()) {
			setErrorMessage("Please maintain the group name");
			valid = false;
		}
		if (groupDescriptionText.getText().isEmpty()) {
			setErrorMessage("Please maintain the group description");
			valid = false;
		}
		if (groupPrivacy == null) {
			setErrorMessage("Please select the group privacy");
			valid = false;
		}
		return valid;
	}

	// We allow the user to resize this dialog
	@Override
	protected boolean isResizable() {
		return true;
	}

	// We need to have the textFields into Strings because the UI gets disposed
	// and the Text Fields are not accessible any more.
	private void saveInput() {
		groupName = groupNameText.getText();
		groupDescription = groupDescriptionText.getText();
		groupPrivacy = dataForPrivacySelectionBox[privacySelection
				.getSelectionIndex()];
		logger.info("selection is:"
				+ privacySelection.getSelectionIndex());

	}

	@Override
	protected void okPressed() {
		super.okPressed();
	}

	public String getGroupName() {
		return groupName;
	}

	public String getDescription() {
		return groupDescription;
	}

	public Exposure getGroupPrivacy() {
		return groupPrivacy;
	}

}
